import pickle
import string

F=open('prime1.txt','r')
l=pickle.load(F)
F.close()

num=set('0123456789')
flag=0
for prime in l:
    s="%i"%prime
    nums=set(s)
    nums=nums.intersection(set('012'))

    flag+=1
    if flag%100==0:
        print "--------",prime,flag
    if not (s.count('1')==3 or s.count('0')==3 or s.count('2')==3 or s.count('0')==6 or s.count('1')==6 or s.count('2')==6):
        continue
    for i in nums:
        count=10
        for j in num:
            if i<>j and count>7:
                news=string.replace(s,i,j)
                news=int(news)
                if news%3==0:
                    count=0
                elif not news in l:
                    count-=1
                else:
                    if news<prime:
                        count-=1
                if count<8:
                    count=0
        if count==8:
            print prime,count,i
            exit()
       
